﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ZX.WebApp.cspModel;

namespace ZX.WebApp.DesignForm.T4Model
{
    public class T4Process
    {
        /// <summary>
        /// 获得数据连接
        /// </summary>
        /// <returns>链接</returns>
        private static SqlConnection GetConnection()
        {
            return new SqlConnection(DBSchema.CommonConnectionString);
        }

        /// <summary>
        /// 根据表名查询表结构信息
        /// </summary>
        private const string TableInfo = @"select * from(
                            SELECT TOP 100 PERCENT --a.id, 
                                  cast( d.name as varchar)  AS EntityName, 
                                  cast(ISNULL(g.[value], '') as nvarchar)AS ColDesc,
                                  cast(a.name as nvarchar) AS ColName, 
                                  cast((a.colorder-1) as int) AS ColSeq,
                                  b.name AS ControlsType,
	                              a.length AS  Width
                            FROM dbo.syscolumns a LEFT OUTER JOIN
                                  dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
                                  dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND 
                                  d.status >= 0 LEFT OUTER JOIN
                                  dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
                                  sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id AND 
                                  g.name = 'MS_Description' LEFT OUTER JOIN
                                  sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 AND 
                                  f.name = 'MS_Description'
                            ORDER BY d.name) a where EntityName='{0}'";
        /// <summary>
        /// 根据表名获取表结构信息
        /// </summary>
        /// <param name="tableName">表名</param>
        public static DataTable GetTableSchema(string tableName)
        {
            DataTable dt;
            using (SqlConnection con = GetConnection())
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format(TableInfo, tableName);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                dt = ds.Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 返回初始化页面代码
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="formname">表单名</param>
        /// <param name="Model">模板名</param>
        /// <returns>表单初始化页面代码</returns>
        public string CreateHTMLbyTT(string table,string formname,string Model)
        {
            DataTable dt = GetTableSchema(table);
            switch(Model)
            {
                case "UI":
                UI u = new UI(dt,formname);
                return u.TransformText();

                case "UIandDetail":
                UIandFormatDetail ud = new UIandFormatDetail(dt, formname, formname);
                return ud.TransformText();

                case "UIandTable":
                UIandTable ut = new UIandTable(dt, formname);
                return ut.TransformText();

                default: return "";
            }
        }


        
    }

   

}